package logic

import (
	"context"
	"my-zero-tiktok/apps/message/models"
	"my-zero-tiktok/common/xerr"

	"my-zero-tiktok/apps/message/pb/pb"
	"my-zero-tiktok/apps/message/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddMessageListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddMessageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddMessageListLogic {
	return &AddMessageListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AddMessageList 批量新增
func (l *AddMessageListLogic) AddMessageList(in *pb.AddListRequest) (*pb.Empty, error) {
	messageList := make([]*models.Message, len(in.List))
	for i, item := range in.List {
		messageList[i] = &models.Message{FromId: item.FromId, ToId: item.ToId, Content: item.Content}
	}

	err := l.svcCtx.MessageModel.BatchInsert(l.ctx, messageList)
	if err != nil {
		return nil, xerr.New(xerr.MessageAddListError)
	}
	return &pb.Empty{}, nil
}
